#import "Page4.h"
#import "MainView.h"

@interface MyUIPickerView: UIPickerView {
	int lastSelected;
}
-(BOOL)table:(UIPickerTable*)table canSelectRow:(int)row;
@end

@implementation MyUIPickerView

// this seems to be the only SEL sent when a row is tapped.
// probably ought to return YES, and close picker
-(BOOL) table:(UIPickerTable*)table canSelectRow:(int)row {
	if (lastSelected != row) {
		lastSelected = row;
//		dolog("table:canSelectRow:%i",row);
	}
    return YES;
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    //dolog("UIDatePicker: respondsToSelector: selector = %s", [NSStringFromSelector(aSelector) UTF8String]);
    return [super respondsToSelector:aSelector];
}

- (void)_sendCheckedRow:(int)row inTable:(UIPickerTable*)table checked:(BOOL)checked {
	dolog("checked %i", row);
	[_delegate selected:row];
	[super _sendCheckedRow:row inTable:table checked:checked];
}
@end

@implementation Page4
- (id)initWithFrame:(CGRect)rect processor:(HTTPProcessor *)_processor {
    if ((self == [super initWithFrame:rect]) != nil) {
		[_processor retain];
		processor = _processor;
	    UIPickerView *refreshPicker = [[MyUIPickerView alloc] initWithFrame:CGRectMake(0.0f, 0, 190.0f, 0.0f)];
	    [refreshPicker setDelegate:self];
	    [refreshPicker setSoundsEnabled:NO];
	    
	    UIPickerTable *_table = [refreshPicker createTableWithFrame: CGRectMake(100.0f, 0.0f, 190.0f, 200.0f)];
	    [_table setAllowsMultipleSelection:NO];

	    UITableColumn *_pickerCol = [[UITableColumn alloc] initWithTitle: @"Refresh" identifier:@"refresh" width: rect.size.width];
	    [refreshPicker columnForTable:_pickerCol];
	        
	    [self addSubview:refreshPicker];
	}
	
	return self;
}

- (void)dealloc {
	[processor release];
    [self dealloc];
    [super dealloc];
}

- (void) selected:(int)row {
	[processor request:[NSString stringWithFormat:@"set-C1-%d",row + 14] delegate:self];
}

- (void) reply:(NSString *)reply {
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(int)row inComponent:(int)component {
	dolog("selected:%i",row);
}

- (int) numberOfColumnsInPickerView:(UIPickerView*)picker {
     return 1;
}

- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)column {
    return 17;
}

- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)column {
    UIPickerTableCell *cell = [[UIPickerTableCell alloc] initWithFrame: CGRectMake(20.0f, 0.0f, 190.0f, 32.0f)];
    [cell setTitle:[NSString stringWithFormat:@"%d", row + 14]];
    return cell;
}

- (void) error:(NSString *)text {
	UIAlertSheet *alertSheet = [ [ UIAlertSheet alloc ]
		initWithFrame: CGRectMake(0, 200, 320, 240) ];
	[ alertSheet setTitle:@"Error:" ];
	[ alertSheet setBodyText: text ];
	[ alertSheet setDelegate: self ];
	[ alertSheet setAlertSheetStyle: 0 ];
	[ alertSheet addButtonWithTitle:@"OK" ];
	[ alertSheet presentSheetInView: self ];
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
	[ sheet dismiss ];
}
@end
